package zz.extjs.util;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FindFile {	
	
	    /** *
	     * 在指定的绝对路径中查找指定的字符串
	     * @param path 绝对路径
	     * @param srcStr 要查找的字符串
	     * 返回文件名List（不包括全路径）
	     */
	    public static List<String> searchPath(String path,String srcStr)
	    {
	        File file=new File(path);
	        List<String> filepaths = new ArrayList<String>();
	        if(file.isDirectory())
	        {
	            File[] fileList=file.listFiles();
	            for(int i=0;i<fileList.length;i++)
	            {
	                if(fileList[i].isDirectory())//是目录就调用递归
	                {
	                    searchPath(fileList[i].getAbsolutePath(),srcStr);
	                }
	                else 
	                {
	                	String str = fileList[i].getName().toLowerCase();
	                	if (str.indexOf(srcStr.toLowerCase()) >=0)
	                	filepaths.add(fileList[i].getName());
	                }
	            }
	        }
	        else if (file.getAbsolutePath().toLowerCase().indexOf(srcStr.toLowerCase()) >=0)
	        {
	            filepaths.add(file.getName());
	        }   
	        return filepaths;
	    }

	   
	public static String getParentPath(String FilePath){
		File file = new File(FilePath);
		return file.exists() ? file.getParentFile().getPath() : null;
		
	}
	
	/**
	 * 为新文件起名
	 * FilePath   保存目录，不带文件名
	 * pbid    护照号码 “IC1134”
	 * ext    扩展名
	 * 返回		“IC1134-X” X是‘0’-‘9’，按顺序检查空余
	 */
	public static File getNewFileName(String FilePath, String pbid, String ext){
		File desfile = null;
		for(int i=0; i<10; i++){
			desfile = new File(FilePath+"/"+pbid+"-"+i+"."+ext);
			if(!(desfile.exists()))	break;
			desfile = null;
		}
		return desfile;
	}

	public static void findFile(String FilePath) {

		File file = new File(FilePath);

		BufferedWriter bw = null;

		if (file.isFile()) {

			StringBuffer sb = new StringBuffer();
			String filePath = file.getPath();
			String filepathshang = file.getParent();
			String filename = file.getName();

			sb.append(filepathshang + "***" + filename);

			try {
				bw = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream("d://TEMP.txt", true), "GBK"));

				bw.write(sb.toString());

				bw.write("\r\n");

				bw.flush();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (bw != null) {
					try {
						bw.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} else if (file.isDirectory()) {

			File f[] = file.listFiles();
			int countOfFile = f.length;
			for (int i = 0; i < countOfFile; i++) {
				findFile(f[i].toString());
			}

		}
	}

	public static List SelectManagerOfFiel(String filehouzhui) {

		//Map<String, String> map = new HashMap<String, String>();

		List list = new ArrayList();

		BufferedReader br = null;

		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("(.)*(\\*){3}([0-9a-zA-Z\u4e00-\u9fa5-_])*\\.([a-zA-Z]){3,4}");
		try {

			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					"d://TEMP.txt")));
			String temp;
			while ((temp = br.readLine()) != null) {
				String str = temp;
				System.out.println(str);
				m = p.matcher(str);
				if (m.find()) {
					String str_find = m.group(0);
					System.out.println("======================" + str_find);
					list.add(str_find);
				}
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
}
